package com.df.common.config;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class MybatisObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasGetter("createDate") && metaObject.getValue("createDate") == null) {
            setFieldValByName("createDate", new Date(), metaObject);
        }
        if (metaObject.hasGetter("createUser") && StpUtil.isLogin()) {
            setFieldValByName("createUser", StpUtil.getLoginId(), metaObject);
        }
        if (metaObject.hasGetter("createTime") && metaObject.getValue("createTime") == null) {
            setFieldValByName("createTime", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasGetter("updateDate") && metaObject.getValue("updateDate") == null) {
            setFieldValByName("updateDate", new Date(), metaObject);
        }
        if (metaObject.hasGetter("updateUser") && StpUtil.isLogin()) {
            setFieldValByName("updateUser", StpUtil.getLoginId(), metaObject);
        }
        if (metaObject.hasGetter("releaseTime") && metaObject.getValue("releaseTime") == null) {
            setFieldValByName("releaseTime", new Date(), metaObject);
        }

    }
}